(function () {
    let newData = [];
    let pageid = 1;
    function render(data = []) {
        newData = [...newData,...data]
        console.log(newData);
        let html = ""

        newData.forEach(item => {
            let reg = /(?<=imgurl=)[^'"]*/;
            let r = item.productImg2.match(reg)
            console.log(r)
            html += `
            <li>
            <img src=${r[0]} width="100">
            <b>${item.productName}</b>
            </li>`
        })
        $('#list').html(html)
    }

    function lazyLoad(fn,threshold = 5) {
        let flag = true ;
        window.onscroll = function () {
            let clientHeight = document.documentElement.clientHeight;
            let scrollTop = document.documentElement.scrollTop;
            let scrollHeight = document.documentElement.scrollHeight;

            let distance = scrollHeight - (clientHeight+scrollTop)

            if (distance < threshold) {
                if (flag) {
                    flag = false;
                    fn()
                }
            }else{
                flag = true;
            }
        }
    }
    lazyLoad(function () {
        pageid++
        if (pageid > 14) {
            return
        }
        $.ajax({
            url:"http://chst.vip:1234/api/getmoneyctrl",
            data: {
                pageid
            }
        })
            .then(res => {
                console.log(res);
                render(res.result)
            })
    },50)

    $.ajax({
        url: "http://chst.vip:1234/api/getmoneyctrl",
            data: {
                pageid: 1
            }
    })
    .then(res => {
        console.log(res);
        render(res.result)
        })
})
()

let ul = document.querySelector('#list')
let el = "";
fetch('http://chst.vip:1234/api/getmoneyctrl')
.then(body => body.json())
.then(res => {
    console.log(res)
    res.result.forEach(item => {
        let regExp = /(?<=imgurl=)[^'"]*/;
        let r = item.productImg2.match(regExp);
        el += `<li productid=${item.productId}>
        <img src=${r[0]} height="100" alt="">
        <b>${item.productName}</b>
    </li>`
    })
    ul.innerHTML = el;
    ul.onclick=function(e){
        if(e.target.localName=="li"){
            let _this = e.target;
            console.log(_this)
            let id =_this.getAttribute('productid')
            location.href='saveDetail.html?productid='+ id
        }
    }
})